#pragma once
#define USEWEBCAM

#include <iostream>
#include <vector>


#include "ofMain.h"
#define OF_ADDON_USING_OFXOPENCV


#include "ofxOpenCv/ofxCvMain.h"
//#include "ofxOpenCv/ofCvMain.h"
#include "ofCvCameraCalibration.h"
#include "ofxVectorMath/ofxVectorMath.h"
#include "coordWarp.h"

class osCamerahandler : public ofCvBlobListener
{
public:
	osCamerahandler(void);
	~osCamerahandler(void);
	void init(CvSize size);
	void update();
	ofCvColorImage  colorImg;
	
	coordWarping coordwarper;
	
	CvSize origsize;
	//ofFBOTexture backgroundTexture;
	
	int threshold;
	int defaultThreshold;
	ofCvBlobTracker blobTracker;
	vector<ofCvTrackedBlob> trackedBlobs;
	vector<ofPoint> calibPoints;
	CvSize size;

	ofCvGrayscaleImage 	grayImage;
	ofCvGrayscaleImage 	bufferImage;
	ofCvGrayscaleImage 	grayBg;
	ofCvGrayscaleImage 	grayDiff;

	ofCvContourFinder 	contourFinder;

	int gaussblur;
	bool				bLearnBakground;

	int chessboardoffsetx;
	int chessboardoffsety;

	ofCvCameraCalibration calib;

	bool isCalibrating;
	bool isManualCalibrating;
	bool isLearningBackground;

	void blobOn( int x, int y, int id, int order );
	void blobMoved( int x, int y, int id, int order );    
	void blobOff( int x, int y, int id, int order ); 

	void findwarppoints(vector<ofPoint>);
	ofVideoGrabber  vidGrabber;
	bool warpLivefeed;
	ofPoint worldpoints[4];
	ofPoint screenpoints[4];
	
	int minBlobSize;
	int maxBlobSize;

	int blobTTL;
	void draw();
	void drawCheckerboard();
	void callibrate();
	void startManualCallibrate();
};
